<?php
/**
 * Created by PhpStorm.
 * User: guokunsheng
 * Date: 2019/2/20
 * Time: 10:51
 */

namespace app\api\controller\v9;

use app\api\controller\v2\Auth;
use app\common\model\AstroSettingUser;
use app\common\model\AstroSettingUser175;

/**
 * 星盘配置相关接口
 * Class Astroset
 * @package app\api\controller\v8
 */
class Astroset extends Auth
{
    /**
     * 获取星盘配置
     * @method GET
     * @return \think\response\Json
     */
    public function index()
    {
        if (!request()->isGet())
        {
            return show(config('code.error'), '未知操作', [], 403);
        }
        $param = request()->only(['tid', 'chart']);

        $validate = validate('Astroset');

        if (!$validate->scene('index')->check($param))
        {
            return show(config('code.error'), (string)$validate->getError());
        }

        $map = ['uid' => $this->user['id'], 'chart' => $param['chart'], 'tid' => $param['tid']];

        $AstroSettingUserModel = new AstroSettingUser175();

        $data = $AstroSettingUserModel->getUserSettingByTid($map);

        return show(config('code.success'), '加载成功', $data);
    }

    /**
     * 更新星盘配置
     * @method POST
     * @return \think\response\Json
     */
    public function update()
    {
        //新增盘只有整宫制

        if (!request()->isPost())
        {
            return show(config('code.error'), '未知操作', [], 403);
        }
        $param = request()->param();

        $decodeValue = json_decode($param['chart_value'], true);

        $decodeValue['chart'] = $param['chart'];

        $check = $this->checkParam($decodeValue);

        if ($check !== true)
        {
            return show(config('code.error'), $check);
        }

        if ($param['chart'] === 'firdaria' && !isset($decodeValue['firdaria']))
        {
            $decodeValue['firdaria'] = 0;
        }

        unset($decodeValue['chart']);

        $param['chart_value'] = json_encode($decodeValue, true);

        $validate = validate('Astroset');

        if (!$validate->scene('update')->check($param))
        {
            return show(config('code.error'), (string)$validate->getError());
        }

        $param['uid'] = $this->user['id'];

        if (!(new AstroSettingUser175())->updateUserSetting($param))
        {
            return show(config('code.error'), '更新配置失败');
        }

        return show(config('code.success'), '更新配置成功');
    }

    /**
     * 还原星盘配置
     * @method DELETE
     * @return \think\response\Json
     */
    public function restore()
    {
        if (!request()->isPut())
        {
            return show(config('code.error'), '未知操作', [], 403);
        }
        $param = request()->only(['tid', 'chart']);

        $validate = validate('Astroset');

        if (!$validate->scene('delete')->check($param))
        {
            return show(config('code.error'), (string)$validate->getError());
        }

        $param['uid'] = $this->user['id'];

        if (!(new AstroSettingUser175())->restoreUserSetting($param))
        {
            return show(config('code.error'), '还原失败');
        }

        return show(config('code.success'), '还原成功');
    }


    /**
     * 验证客户端传递的参数是否正确
     * @param array $param
     * @return bool|string
     */
    private function checkParam($param = [])
    {
        if (!isset($param['chart']) || !in_array($param['chart'], config('astroset175.chart')))
        {
            return '未知星盘';
        }

        //包含界主星和十度主星的星盘
        $ContainBoundDecanatesCharts = ['natal', 'now', 'profection', 'firdaria', 'fortune', 'zodrel', 'dodekatemorion', 'trimsamsa'];

        //包含希腊点的星盘
        $ContainArabicsCcharts = ['natal', 'now', 'profection', 'firdaria', 'fortune', 'zodrel', 'dodekatemorion', 'trimsamsa', 'transits'];


        //黄道星释盘验证
        if ($param['chart'] === 'zodrel' && (!isset($param['push_fortune']) || count($param['push_fortune']) > 20))
        {
            return '推运基点参数错误';
        }

        //小限盘参数验证
        if ($param['chart'] === 'profection' && (!isset($param['profection_push_direction']) || !in_array($param['profection_push_direction'], [0, 1])))
        {
            return '小限推运方向参数错误';
        }

        if (in_array($param['chart'], $ContainBoundDecanatesCharts) && (!isset($param['extent_type']) || !in_array($param['extent_type'], [0, 1])))
        {
            return '界主星参数错误';
        }

        if (in_array($param['chart'], $ContainBoundDecanatesCharts) && (!isset($param['decanates']) || !in_array($param['decanates'], [0, 1])))
        {
            return '十度主星参数错误';
        }

        if (in_array($param['chart'], $ContainArabicsCcharts) && (!isset($param['arabics']) || count($param['arabics']) > 9))
        {
            return '希腊点参数错误';
        }


        return true;
    }
}
